package me.monster.smarttraffic.ui.dash;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import com.google.android.material.navigation.NavigationView;

import java.util.Map;

import butterknife.BindView;
import me.monster.smarttraffic.R;
import me.monster.smarttraffic.base.AbstractPresenterActivity;
import me.monster.smarttraffic.bo.DataHolder;
import me.monster.smarttraffic.tool.ImageLoader;
import me.monster.smarttraffic.ui.bus.BusActivity;
import me.monster.smarttraffic.ui.car.CarActivity;
import me.monster.smarttraffic.ui.env.EnvActivity;
import me.monster.smarttraffic.ui.info.InfoActivity;
import me.monster.smarttraffic.ui.parking.ParkingActivity;
import me.monster.smarttraffic.ui.road.RoadActivity;
import me.monster.toolbarhelper.toolview.ToolView;
import me.monster.toolbarhelper.toolview.tools.ToolClickListener;

/**
 * @author jiangjiwei
 */
public class DashActivity extends AbstractPresenterActivity<DashPresenter> implements DashContract.View {

    @BindView(R.id.tv_dash_fake)
    TextView tvDashFake;
    @BindView(R.id.tv_dash_environment)
    TextView tvEnvironment;
    @BindView(R.id.tv_dash_bus_distance)
    TextView tvDistance;
    @BindView(R.id.nav_view)
    NavigationView navView;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    @BindView(R.id.tool_single_tool)
    ToolView toolDash;
    DataHolder dataHolder = DataHolder.getInstance();

    public static void start(Context context) {
        Intent starter = new Intent(context, DashActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter.copyVideo();
        toolDash.setListener(new ToolClickListener() {
            @Override
            public void onClick(int what) {
                if (what == ToolView.navigation) {
                    drawerLayout.openDrawer(GravityCompat.START);
                }
            }
        });
        configNavigation();
    }

    private void configNavigation() {
        View headerView = navView.getHeaderView(0);
        ImageView ivHeader = headerView.findViewById(R.id.iv_navigation_avatar);
        TextView tvName = headerView.findViewById(R.id.tv_navigation_account);
        TextView tvQuote = headerView.findViewById(R.id.tv_navigation_sub);
        tvName.setText(dataHolder.getAccount());
        tvQuote.setText(dataHolder.getQuote());
        ImageLoader.loadAvatar(dataHolder.getAvatar(), ivHeader);

        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.nav_dash_c:
                        CarActivity.start(DashActivity.this);
                        break;
                    case R.id.nav_dash_r:
                        RoadActivity.start(DashActivity.this);
                        break;
                    case R.id.nav_dash_p:
                        ParkingActivity.start(DashActivity.this);
                        break;
                    case R.id.nav_dash_b:
                        BusActivity.start(DashActivity.this);
                        break;
                    case R.id.nav_dash_e:
                        EnvActivity.start(DashActivity.this);
                        break;
                    case R.id.nav_dash_i:
                        InfoActivity.start(DashActivity.this);
                        break;
                    default:
                }
//                drawerLayout.closeDrawer(GravityCompat.START);
                return true;
            }
        });
    }

    @Override
    public void showError(int error) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.fetchInfo();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        drawerLayout.closeDrawer(GravityCompat.START);
    }

    @Override
    public DashPresenter createPresenter() {
        return new DashPresenter();
    }

    @Override
    public View closeToolView() {
        return null;
    }

    @Override
    public String getInitTitle() {
        return "";
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_dash;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent home = new Intent(Intent.ACTION_MAIN);
            home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            home.addCategory(Intent.CATEGORY_HOME);
            startActivity(home);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void dismissLoading() {

    }

    @Override
    public void updateInfo(Map<String, String> map) {
        tvEnvironment.setText(getString(R.string.dash_main_road, map.get("pm"), map.get("temperature"), map.get("humidity"), map.get("co")));
        tvDistance.setText(getString(R.string.dash_main_bus, map.get("bus1_1"), map.get("bus1_2"), map.get("bus2_1"), map.get("bus2_2")));
    }
}
